#include "RC_receive.h"
#include "main.h"

//负责CAN端的遥控器解析 并将数据存入遥控器的数据结构体当中

RC_ctrl_t rc_ctrl;

const RC_ctrl_t *get_rc_ctrl_point(void) //获取遥控器数据指针
{
    return &rc_ctrl;
}

void sbus_to_rc(volatile const uint8_t *sbus_buf) //遥控数据解析
{
    if(sbus_buf == NULL)
    {
        return;
    }

    //解析遥控器数据
    rc_ctrl.rc.ch[0] = (sbus_buf[0] | (sbus_buf[1] << 8)) & 0x07ff; //!< Channel 0
    rc_ctrl.rc.ch[1] = ((sbus_buf[1] >> 3) | (sbus_buf[2] << 5)) & 0x07ff; //!< Channel 1
    rc_ctrl.rc.ch[2] = ((sbus_buf[2] >> 6) | (sbus_buf[3] << 2) | (sbus_buf[4] << 10)) &0x07ff; //!< Channel 2
    rc_ctrl.rc.ch[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3
    rc_ctrl.rc.s[0] = ((sbus_buf[5] >> 4) & 0x0003); //!< Switch left
    rc_ctrl.rc.s[1] = ((sbus_buf[5] >> 4) & 0x000C) >> 2; //!< Switch right
    rc_ctrl.mouse.x = sbus_buf[6] | (sbus_buf[7] << 8); //!< Mouse X axis
    rc_ctrl.mouse.y = sbus_buf[8] | (sbus_buf[9] << 8); //!< Mouse Y axis
    rc_ctrl.mouse.z = sbus_buf[10] | (sbus_buf[11] << 8); //!< Mouse Z axis
    rc_ctrl.mouse.press_l = sbus_buf[12]; //!< Mouse Left Is Press ?
    rc_ctrl.mouse.press_r = sbus_buf[13]; //!< Mouse Right Is Press ?
    rc_ctrl.key.v = sbus_buf[14] | (sbus_buf[15] << 8); //!< KeyBoard value
    rc_ctrl.rc.ch[4] = sbus_buf[16] | (sbus_buf[17] << 8);//!< Channel 4

    //遥控物理中心数值偏移补偿修正
    rc_ctrl.rc.ch[0] -= RC_CH0_VALUE_OFFSET;   //右x摇杆
    rc_ctrl.rc.ch[1] -= RC_CH1_VALUE_OFFSET;   //右y摇杆
    rc_ctrl.rc.ch[2] -= RC_CH2_VALUE_OFFSET;   //左x摇杆 
    rc_ctrl.rc.ch[3] -= RC_CH3_VALUE_OFFSET;   //右y摇杆
    rc_ctrl.rc.ch[4] -= RC_CH4_VALUE_OFFSET;   //拨轮

}
